package testDay;

public class day1_2 {
    public int divide(int a, int b) {
        int res = 0;
        //极端情况a为最小值
        if (a == Integer.MIN_VALUE && b == -1) {
            return Integer.MAX_VALUE;
        }
        //判断a，b符号,fValue为1是奇数
        int fValue = 0;
        if (a > 0) {
            a = -a;
            fValue++;
        }
        if (b > 0) {
            b = -b;
            fValue++;
        }
        System.out.println(a);
        System.out.println(b);
        while (a <= b) {
            int count = 1;
            int temp = b;//减数
            //确定减数的倍数temp,temp的2倍不能小于最小值
            while (temp >= Integer.MIN_VALUE / 2 && a <= temp * 2) {
                //位运算左移一位，数值加倍
                temp = temp << 1;
                count = count << 1;
            }
            a -= temp;
            res += count;
        }
        if (fValue == 1) {
            return -res;
        } else {
            return res;
        }
    }
}
